package com.cwt_taxi_xa.protocol;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.util.Log;

import com.cwt_taxi_xa.ActivityACC;
import com.cwt_taxi_xa.ActivityPower;
import com.cwt_taxi_xa.ActivitySPTest;
import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.VideoActivity;
import com.cwt_taxi_xa.VideoActivity2;
import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.constants.MyStatus;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.protocol.can.ConditioningDecode;
import com.cwt_taxi_xa.protocol.can.VehicleInformation;
import com.cwt_taxi_xa.protocol.fm.CarRadio;
import com.cwt_taxi_xa.protocol.mcu.PartyControl;
import com.cwt_taxi_xa.protocol.voice.VoiceTool;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.Tools;
import com.cwt_taxi_xa.util.VolumeControl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MCUUtil {
	static final String TAG = "MCUUtil";
	public static String MCUV = "";

	public static int light;

	public static boolean reH = true;//用于复位PC7测试
	public static ConditioningDecode mConditioningDecode = new ConditioningDecode();
	public static VehicleInformation mVehicleInformation = new VehicleInformation();
	public static VoiceTool mVoiceTool = new VoiceTool();
	public static PartyControl mPartyControl = new PartyControl();
	public static CarRadio mCarRadio = new CarRadio();
	public static void onCardReceive(byte[] data) {


		//AppLog.i("接收MCU数据:"+Tools.parseByte2HexStr(data));
		//Log.i(TAG,"接收MCU数据:"+Tools.parseByte2HexStr(data));
		NianBaoUtil.nb_mcu(data);


	}

	/**
	 * MCU指令打包               添加校验、包尾、转义
	 * @param buffer
	 * @return
	 */
	public static byte[] getMCUData(byte[] buffer){

//		byte[] data = new byte[buffer.length+2];
//		System.arraycopy(buffer, 0, data, 0, buffer.length);
//		data[data.length-2] = (byte) (buffer[0]^buffer[1]);
//		for (int i = 2; i < buffer.length; i++) {
//			data[data.length-2] = (byte) (data[data.length-2]^buffer[i]);
//		}
//		data[data.length-1] = 0x55;
//
//		String zjdata = Tools.parseByte2HexStr2(data);
//		zjdata = zjdata.substring(3, zjdata.length()-2);
//		zjdata = zjdata.replaceAll(" 54 ", " 54 01 ");
//		zjdata = zjdata.replaceAll(" 55 ", " 54 02 ");
//		zjdata = "55"+zjdata+"55";
//		return Tools.parseHexStr2Byte(zjdata);
		return getSerialMCUData(buffer);
	}

	/**
	 * 默认添加头部
	 * @param buffer
	 * @return
	 */
	public static byte[] getSerialMCUData(byte[] buffer){
		byte[] data = new byte[buffer.length+2];
		System.arraycopy(buffer, 0, data, 0, buffer.length);
		data[data.length-2] = (byte) (buffer[0]^buffer[1]);
		for (int i = 2; i < buffer.length; i++) {
			data[data.length-2] = (byte) (data[data.length-2]^buffer[i]);
		}
		data[data.length-1] = 0x55;
		try {
			return mcuReverseSend(data,1,data.length -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	static int accCount = 0;
	static String str_tip;
	public static String stats;
	public static void onOrder(byte[] data) {
		str_tip = Tools.parseByte2HexStr(data);

//		if(!str_tip.contains("7A3E")){
//			AppLog.v("MCU指令:"+str_tip);
//		}

		AppLog.savelog("MCU指令接收： "+str_tip);

		Log.d(TAG,"MCU指令接收： "+Tools.parseByte2HexStr(data));

        data = mcuReverseReceive(data);

		//String str = Tools.parseByte2HexStr2(data);
		//Log.d("MCUUtil","MCU指令接收1 " + "str:" + str);
		//str = str.substring(3, str.length()-2);
		//Log.d("MCUUtil","MCU指令接收2 " + "str:" + str);
		//str = str.replaceAll(" 79 02 ", " 7A ");
		//str = str.replaceAll(" 79 01 ", " 79 ");
		//Log.d("MCUUtil","MCU指令接收3 " + "str:" + str);
		//data = Tools.parseHexStr2Byte(str);


		String mid_str = Tools.parseByte2HexStr(Tools.byteTobyte(data, 1, 1));
		byte[] dataBuffer = Tools.byteTobyte(data, 4, data.length-6);
		String data_str = Tools.parseByte2HexStr(dataBuffer);
//		if(!mid_str.equals("3E")){
//			AppLog.e("MCU指令---命令字："+mid_str+"   数据"+data_str);
//		}
		Log.d("MCUUtil","MCU指令接收 " + "mid_str:" + mid_str + " data_str:" + data_str);

		if(mid_str.endsWith("48")){
			MCUV = new String(Tools.parseHexStr2Byte(data_str));
			MainApplication.topActivity.sendMsg(1015, MCUV);
		}

//		if(mid_str.equals("3E")){
//			AppLog.e("io收到心跳---是否处理："+reH);
//		}
		if(mid_str.equals("3E")&&reH){

			send3EYD();
			//心跳
			stats = data_str.substring(0, 8);
			stats = Tools.byteTobit(Tools.parseHexStr2Byte(stats));
			String dianya = data_str.substring(8, 12);
			if(data_str.length()==14){
				String light_str = data_str.substring(12, 14);
				light = Integer.parseInt(light_str, 16);

//				if(light!=0){
//					if(MainApplication.topActivity!=null){
//						CarCallSystemBiz.setScreenBrightness(MainApplication.topActivity, 25+((light-1)*25));
//						CarCallSystemBiz.saveScreenBrightness(MainApplication.topActivity, 25+((light-1)*25));
//						AppLog.e("设置亮度："+25+((light-1)*25));
//					}
//				}
			}

//			AppLog.e("MCU指令---状态："+stats+"   电压："+dianya);
			AppLog.savelog("MCU----状态： "+stats+"   电压："+Integer.parseInt((dianya), 16)+40);

			if(MainApplication.topActivity!=null){
				MainApplication.topActivity.sendMsg(1012, dianya);

				//判断acc
				if(stats.substring(31, 32).equals("1")){
					accCount = 0;
					if (!ActivityACC.accon) {
						// acc接上
						MainApplication.topActivity.sendMsg(404, null);
						AppLog.e("acc接上");
						AppLog.savelog("-----------acc接上");
					}
				}
				if(stats.substring(31, 32).equals("0")){
					accCount++;
					if(accCount>4){
						accCount = 0;
						if(ActivityACC.accon){
							// acc断开
							MainApplication.topActivity.startActivity(new Intent(
									MainApplication.topActivity, ActivityACC.class));
							AppLog.e("acc断开");
							AppLog.savelog("-----------acc断开");
						}
					}
					MCUUtil.mConditioningDecode.setFuelAlarmInit(false);//警告只提示一次
					MCUUtil.mConditioningDecode.setFuelAlarm(0);//警告恢复默认
				}


				//判断倒车
				if(stats.substring(25, 26).equals("1")){
					if(!(MainApplication.topActivity instanceof VideoActivity2)){
						if(MainApplication.topActivity instanceof VideoActivity){
							MainApplication.topActivity.sendMsg(1014, null);
						}
						AppLog.savelog("-----------大开倒车界面");
						MainApplication.topActivity.startActivity(new Intent(MainApplication.topActivity, VideoActivity2.class));
					}
				}
				if(stats.substring(25, 26).equals("0")){
					if(MainApplication.topActivity instanceof VideoActivity2){
						AppLog.savelog("-----------关闭倒车界面");
						MainApplication.topActivity.sendMsg(1014, null);
					}
				}
			}

			ActivityPower.chemen = stats.substring(29, 30).equals("0");

		}

		if(mid_str.equals("3D")){
			//搜台结果
//			AppLog.e("MCU指令---搜台结果："+data_str);
			data_str = data_str.substring(4);
			String[] result = new String[data_str.length()/8];
			for (int i = 0; i < result.length; i++) {
				result[i] = data_str.substring(8*i, 8*i+8);
			}
			if(MainApplication.topActivity != null){
				MainApplication.topActivity.sendMsg(789, result);
			}
		}else if(mid_str.endsWith("46")){
			int index = Integer.parseInt(data_str.substring(2, 4), 16);
			int all = Integer.parseInt(data_str.substring(0, 2), 16);
			AppLog.e("mcu请求包："+index+"/"+all);
			index--;
			if(index>200){
				AppLog.e("mcu升级结束");
			}else{
				sendUpdata(index);
			}
		}else if(mid_str.equals("30")){
			mPartyControl.decode(dataBuffer);
			//AppLog.e("MCU指令---方控结果：" + voiceDecode);
		}else if(mid_str.equals("31")){
			if("00".equals(data_str)){
				mCarRadio.setReady(true);
				MainApplication.topActivity.sendMsg(790, null);
			}
			AppLog.e("MCU指令---打开收音机结果："+ data_str);
//			TTsFunction.speakText("31,"+data_str);
		}else if(mid_str.equals("6A")){
			decodeArray(dataBuffer);
			AppLog.e("MCU指令---CunBus结果：" + data_str + " ActivitySPTest.SPindex:" + ActivitySPTest.SPindex);
			//Can串口测试
			if(ActivitySPTest.isView && ActivitySPTest.SPindex == 8){
				try {
					MainApplication.topActivity.sendMsg(1017, data);
					return;
				} catch (Exception e) {
					AppLog.savelog("接收到读卡器数据发送串口测试界面-----------------：报错\n"+e.getMessage());
				}
			}
		}else if(mid_str.equals("F5")){
			AppLog.e("MCU指令---声耕结果：" + data_str);
			mVoiceTool.onReceiveOrder(dataBuffer);
			//声耕串口测试
			if(ActivitySPTest.isView && ActivitySPTest.SPindex == 9){
				try {
					MainApplication.topActivity.sendMsg(1017, data);
					return;
				} catch (Exception e) {
					AppLog.savelog("接收到读卡器数据发送串口测试界面-----------------：报错\n"+e.getMessage());
				}
			}
		}
//		else if(mid_str.equals("32")){
//			//切换声道结果
//			AppLog.e("MCU指令---切换声道结果："+data_str);
//		}
//		else if(mid_str.equals("3D")){
//			//搜台结果
////			AppLog.e("MCU指令---搜台结果："+data_str);
//			data_str = data_str.substring(4);
//			String[] result = new String[data_str.length()/8];
//			for (int i = 0; i < result.length; i++) {
//				result[i] = data_str.substring(8*i, 8*i+8);
//			}
//			MainApplication.topActivity.sendMsg(789, result);
//		}
//		else if(mid_str.endsWith("46")){
//			int index = Integer.parseInt(data_str.substring(2, 4), 16);
//			int all = Integer.parseInt(data_str.substring(0, 2), 16);
//			AppLog.e("mcu请求包："+index+"/"+all);
//			index--;
//			if(index>200){
//				AppLog.e("mcu升级结束");
//			}else{
//				sendUpdata(index);
//			}
//		}
//
//		else if(mid_str.endsWith("48")){
//			MCUV = new String(Tools.parseHexStr2Byte(data_str));
//			MainApplication.topActivity.sendMsg(1015, MCUV);
//		}
//
//		else{
////			AppLog.e("MCU接收："+data_str);
//		}
	}

	/*
	 * Can数据解析
	 */
	public static void decodeArray(byte[] buffer){
		int num = buffer[0] & 0xff;
		for(int i = 0 ;i< num; i++){
			byte[] frameBuffer = new byte[12];
			System.arraycopy(buffer, 1 + i * frameBuffer.length, frameBuffer, 0, frameBuffer.length);
			String cmdId =  Tools.parseByte2HexStr(Tools.byteTobyte(frameBuffer, 0, 4));
			AppLog.d(TAG, "decodeArray cmdId:" + cmdId);
			//调试专用
			if (ConditioningDecode.CAN_ID.equals(cmdId)) {
				mConditioningDecode.decode(frameBuffer);
			}else if(ConditioningDecode.CAN_HEATER_FUEL_ID.equals(cmdId)){
				mConditioningDecode.decodeHeaterFuel(frameBuffer);
			}else if(ConditioningDecode.CAN_HEATER_TYPE_ID.equals(cmdId)){
				mConditioningDecode.decodeHeaterType(frameBuffer);
			}else{
				mVehicleInformation.decode(cmdId,frameBuffer);
			}
		}
	}

	private static void send3EYD() {
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 3E 0003 00")));
//		AppLog.e("------------------------------send3EYD");
	}

	/**
	 * 切换声道
	 * @param chl 1-FM  2-系统   3-AUX   4-外部音源
	 */
	public static void selecterVic(int chl){
		MyStatus.vichl = chl;
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 32 0003 0"+chl)));
		AppLog.e("设置声道："+chl + " generateTag:" + AppLog.generateTag(3));
	}

	/**
	 * 透传串口5数据
	 * @param data
	 * @param data
	 */
	public static byte[] touchuan(byte[] data){
		byte[] body = new byte[data.length+4];
		body[0] = 0x55;
		body[1] = (byte) 0xF5;
		System.arraycopy(Tools.int2Bytes2(data.length+2, 2), 0, body, 2, 2);
		System.arraycopy(data, 0, body, 4, data.length);
		return body;
	}

	public static void checkV(){
		//AppLog.e("MCU"+AppLog.generateTag());
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 47 0002")));
	}

	public static void InsMusic(Context context){
		try {

			AssetManager assetManager = context.getAssets();
			InputStream asis = assetManager.open("Music");
			File f = new File(Constants.SDPath+"Music.apk");
			FileOutputStream fos = new FileOutputStream(f);
			byte[] buffer = new byte[1024];
			int busize = 0;
			while ((busize = asis.read(buffer)) != -1) {
				fos.write(buffer, 0, busize);
			}
			fos.flush();
			fos.close();

			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
			context.startActivity(intent);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static void Insdida(Context context){
		try {

			AssetManager assetManager = MainApplication.topActivity.getAssets();
			InputStream asis = assetManager.open("dida");
			File f = new File(Constants.SDPath+"DIDA.apk");
			FileOutputStream fos = new FileOutputStream(f);
			byte[] buffer = new byte[1024];
			int busize = 0;
			while ((busize = asis.read(buffer)) != -1) {
				fos.write(buffer, 0, busize);
			}
			fos.flush();
			fos.close();

			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
			context.startActivity(intent);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void UpV(){
		try {
//			File f=new File(Constants.SDPath+"/CWT-1600-MCU-20181220-V003(2).bin");

			AssetManager assetManager = MainApplication.topActivity.getAssets();
//			InputStream asis = assetManager.open("CUBE_YHHYANG(1).bin");
			InputStream asis = assetManager.open(Constants.MCU_FILE);
//			InputStream asis = assetManager.open("CWT-1600-MCU-20190220-V011.bin");
//			InputStream asis = assetManager.open("CWT-1600-MCU-20190115-V010.bin");
//			InputStream asis = assetManager.open("CWT-1600-MCU-20190111-V007.bin");
			File f = new File(Constants.SDPath+"MCUFILE");
			FileOutputStream fos = new FileOutputStream(f);
			byte[] buffer = new byte[1024];
			int busize = 0;
			while ((busize = asis.read(buffer)) != -1) {
				fos.write(buffer, 0, busize);
			}
			fos.flush();

			int length=(int)f.length();
			byte[] buff=new byte[length];
			FileInputStream fin=new FileInputStream(f);
			fin.read(buff);
			fin.close();

			crc_UpFile = getcrc16_ccitt_false(buff);

			int count_b = length/1024;
			if(length%1024!=0) count_b++;
			data_UpFile = new byte[count_b][];
			for (int i = 0; i < data_UpFile.length; i++) {
				if(i==data_UpFile.length-1&&length%1024!=0){
					data_UpFile[i] = new byte[length%1024];
				}else{
					data_UpFile[i] = new byte[1024];
				}
				System.arraycopy(buff, i*1024, data_UpFile[i], 0, data_UpFile[i].length);
			}
			AppLog.e("mcu升级-总包数："+count_b);
			sendUpdata(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void UpV2(){
		try {
//			File f=new File(Constants.SDPath+"/CWT-1600-MCU-20181220-V003(2).bin");

			AssetManager assetManager = MainApplication.topActivity.getAssets();
//			InputStream asis = assetManager.open("CWT-1600-MCU-20181221-V001.bin");
			InputStream asis = assetManager.open("CWT-1600-MCU-20181229-V002.bin");
			File f = new File(Constants.SDPath+"MCUFILE");
			FileOutputStream fos = new FileOutputStream(f);
			byte[] buffer = new byte[1024];
			int busize = 0;
			while ((busize = asis.read(buffer)) != -1) {
				fos.write(buffer, 0, busize);
			}
			fos.flush();

			int length=(int)f.length();
			byte[] buff=new byte[length];
			FileInputStream fin=new FileInputStream(f);
			fin.read(buff);
			fin.close();

			crc_UpFile = getcrc16_ccitt_false(buff);

			int count_b = length/1024;
			if(length%1024!=0) count_b++;
			data_UpFile = new byte[count_b][];
			for (int i = 0; i < data_UpFile.length; i++) {
				if(i==data_UpFile.length-1&&length%1024!=0){
					data_UpFile[i] = new byte[length%1024];
				}else{
					data_UpFile[i] = new byte[1024];
				}
				System.arraycopy(buff, i*1024, data_UpFile[i], 0, data_UpFile[i].length);
			}
			AppLog.e("mcu升级-总包数："+count_b);
			sendUpdata(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static byte[][] data_UpFile;
	public static byte[] crc_UpFile;
	public static void sendUpdata(int index){
		byte[] sendata = new byte[8+data_UpFile[index].length];
		sendata[0] = 0x55;
		sendata[1] = 0x45;
		System.arraycopy(Tools.int2Bytes2(data_UpFile[index].length+6, 2), 0, sendata, 2, 2);
		System.arraycopy(Tools.int2Bytes2(data_UpFile.length, 1), 0, sendata, 4, 1);
		System.arraycopy(Tools.int2Bytes2(index+1, 1), 0, sendata, 5, 1);
		System.arraycopy(crc_UpFile, 0, sendata, 6, 2);
		System.arraycopy(data_UpFile[index], 0, sendata, 8, data_UpFile[index].length);
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(sendata));
	}

    public static byte[] mcuReverseReceive(byte[] bytes){
        int size = bytes.length;
        byte[] data_order = new byte[size];
        int index = 0;
        for (int i = 0; i < size; i++) {
            if(bytes[i] == 0x79){
                if( i+1 < size){
                    if(bytes[i+1]==1){//需要判断数组越界
                        data_order[index] = 0x79;//0x79<————> 0x79 后紧跟一个0x01
                    }else{
                        data_order[index] = 0x7A;//0x7a <————> 0x79 后紧跟一个0x02
                    }
                }else{
                    data_order[index] = bytes[i];
                }
                i++;
            }else{
                data_order[index] = bytes[i];
            }
            index++;
        }

        return Tools.byteTobyte(data_order, 0, index);
    }

    private static byte[] mcuReverseSend(byte[]  buffer ,int start ,int end){
        byte[] newBuffer = new byte[buffer.length * 2];
        if(buffer != null){
            for (int i = 0; i < start; i++) {
                newBuffer[i] = buffer[i];
                //Log.d(TAG,"start i:" + i);
            }
            int expandLength = 0;
            for (int i = start; i < end; i++) {
                if (buffer[i] == 0x54) {
                    //0x54 <————> 0x54 后紧跟一个0x01
                    //0x55 <————> 0x54 后紧跟一个0x02
                    newBuffer[i + expandLength] = 0x54;
                    newBuffer[i + ++expandLength] = 0x01;
                }else if (buffer[i] == 0x55) {
                    newBuffer[i + expandLength] = 0x54;
                    newBuffer[i + ++expandLength] = 0x02;
                } else {
                    newBuffer[i + expandLength] = buffer[i];
                }

            }
            for (int i = end; i < buffer.length; i++) {
                //Log.d(TAG,"end i:" + i);
                newBuffer[i + expandLength] = buffer[i];
            }
            //Log.d(TAG,"mcuReverseSend newBuffer:" + Tools.parseByte2HexStr(newBuffer));
            return Tools.byteTobyte(newBuffer,0,buffer.length + expandLength);
        }
        Log.e(TAG,"error !!!!!!!!!!!! mcuReverseSend buffer:" + Tools.parseByte2HexStr(buffer));
        return buffer;
    }

	public static void dataSerialConfig(){
		AppLog.e("SerialConfig: 05 00 01 C2 00");
		//serialConfig(Tools.parseHexStr2Byte("05 00 01 C2 00"));
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 69 0007 05 00 01 C2 00")));
		//设置串口115200
		//55 69 0007 05 00 01 C2 00 FD55
		//7AF5000BAA55A10901010009 257A
	}

	public static byte[] getcrc16_ccitt_false(byte[] buf) {
		int crc = 0xFFFF;
		for (int j = 0; j < buf.length; j++) {
			crc = ((crc >>> 8) | (crc << 8)) & 0xffff;
			crc ^= (buf[j] & 0xff);
			crc ^= ((crc & 0xff) >> 4);
			crc ^= (crc << 12) & 0xffff;
			crc ^= ((crc & 0xFF) << 5) & 0xffff;
		}
		crc &= 0xffff;

		byte[] crcByte = new byte[2];
		crcByte[0] = (byte) ((crc >> 8)& 0xff);
		crcByte[1] = (byte) (crc & 0xff);

		return crcByte;
	}






	/**
	 * 注意*传进来的值必须是1~4,包括1和4*
	 * 控制IO脚
	 * 	01:油路控制
	 02:空调控制
	 03：预留1控制
	 04：预留2控制
	 */
	public static void setIO(int io, boolean status){
		AppLog.e("IO控制："+io+"       "+status);
		if(status){
			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 58 0004 0"+io+"01")));
		}else{
			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 58 0004 0"+io+"00")));
		}
	}

	/**
	 * 设置声音开关，打算用于系统通知有音频时再打开---
	 * @param vol
	 */
	public static void hasVic(boolean vol){
		AppLog.e("控制MCU声音开关："+vol);
		if(vol){
			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 4F 0003 01")));
//			setvol(6);
		}else{
			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 4F 0003 00")));
//			setvol(1);
		}
	}

	public static void hasVic2(boolean vol){
		AppLog.e("控制MCU声音开关2："+vol);
		if(vol){
//			selecterVic(2);
//			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 4F 0003 01")));
//			setvol(6);
		}else{
//			selecterVic(3);
//			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 4F 0003 00")));
//			setvol(0);
		}
	}

	public static void openFM(){
//		TTsFunction.speakText("打开FM");
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 31 0003 01")));
	}

	public static void closeFM(){
//		TTsFunction.speakText("关闭FM");
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 31 0003 00")));
	}

	public static void closeFMSearch(){
//		TTsFunction.speakText("关闭FM");
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 36 0002")));
	}

	public static void changeVolume(int chanel ,int volume){
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("553F00040" + String.valueOf(chanel) +(volume>15?"":"0")+Integer.toHexString(volume))));
	}

	public static String[] voltips = new String[50];
	public static int voltipsindex = 0;
	public static SimpleDateFormat sdf_vol;
	public static void setvol(int v, String tip){
//		AppLog.e(tip+"     设置声音："+v);
		if(v<0) v=0;
		if(v>25) v=25;
		try {
			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("553F000401"+(v>15?"":"0")+Integer.toHexString(v))));
			SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("553F000407"+(v>15?"":"0")+Integer.toHexString(v))));
			SPUtils.setParam(SPConstants.SPK_vol, v);

			//用于处理声音忽大忽小问题，已解决，根源是通用线程被重启
			if(voltipsindex>=50){
				for (int i = 0; i < 10; i++) {
					voltips[i] = voltips[i+40];
				}
				voltipsindex = 10;
			}
			if(sdf_vol == null){
				sdf_vol = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}

			voltips[voltipsindex] = sdf_vol.format(new Date())+"          "+tip+"      set:"+v;
			AppLog.savelog("声音日志：   "+voltips[voltipsindex]);
			voltipsindex++;

		} catch (Exception e) {
		}
	}
	static int vol;
	public static void setvolAdd(){
		vol = SPUtils.getParamInteger(SPConstants.SPK_vol, SPConstants.SPV_vol);
		if(vol<25){
			vol++;
			setvol(vol, "音量加setvolAdd");
			VolumeControl.playSound();
		}
	}
	public static void setvolLess(){
		vol = SPUtils.getParamInteger(SPConstants.SPK_vol, SPConstants.SPV_vol);
		if(vol>0){
			vol--;
			setvol(vol, "音量减setvolLess");
			VolumeControl.playSound();
		}
	}

}
